package com.sprucetec.live.common.util;

/**
 * 处理ServiceResult类型的返回值
 */
public class ServiceResultUtil {
	/**
	 * 参数错误
	 * @return
	 */
	public static <T> ServiceResult<T> paramsError() {
		return fail(RespCode.PARAMETER_INVALID);
	}

	public static <T> ServiceResult<T> fail() {
		return fail(RespCode.ERROR);
	}

	public static <T> ServiceResult<T> fail(String msg) {
		return new ServiceResult<>(RespCode.ERROR.getCode(), msg);
	}

	public static <T> ServiceResult<T> fail(RespCode rc) {
		return new ServiceResult<>(rc.getCode(), rc.getDetail());
	}

	public static <T> ServiceResult<T> fail(RespCode rc, String msg) {
		return new ServiceResult<>(rc.getCode(), msg);
	}
	
	public static <T> ServiceResult<T> fail(boolean success, RespCode rc, String msg) {
		ServiceResult<T> sr = new ServiceResult<>();
		sr.setErrorCode(rc.getCode());
		sr.setSuccess(success);
		sr.setMessage(msg);
		return sr;
	}

	public static <T> ServiceResult<T> fail(int code, String msg) {
		return new ServiceResult<>(code, msg);
	}

	public static <T> ServiceResult<T> success() {
		return success(RespCode.SUCCESS);
	}

	public static <T> ServiceResult<T> success(RespCode rc) {
		if (rc == null)
			rc = RespCode.SUCCESS;
		return success(rc, null);
	}

	public static <T> ServiceResult<T> success(RespCode rc, T t) {
		return success(rc.getCode(), rc.getDetail(), t);
	}

	public static <T> ServiceResult<T> success(int code, String msg) {
		return success(code, msg, null);
	}

	public static <T> ServiceResult<T> success(int code, String msg, T t) {
		return success(true, code, msg, t);
	}

	public static <T> ServiceResult<T> success(boolean success, int code, String msg, T t) {
		ServiceResult<T> sr = new ServiceResult<>(t);
		sr.setErrorCode(code);
		sr.setSuccess(success);
		sr.setMessage(msg);
		sr.setBody(t);
		return sr;
	}

	public static <T> ServiceResult<T> success(T t) {
		return success(RespCode.SUCCESS, t);
	}
}
